home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8614 / 8614.xpi / modules / utils / Events.jsm next >
Text File  |  2010-02-10  |  1KB  |  52 lines

  1. // DO NOT import this into the global namespace, but instead
  2. // import it into your own namespace wrapper
  3.  
  4. var EXPORTED_SYMBOLS = ["EventSource"];
  5.  
  6. Components.utils.import("resource://glydo/utils/prototype_xul_1_6_0_3_modified.jsm");
  7.  
  8. var EventSource = Prototype.Class.create({
  9.     initialize: function() {
  10.         this.listeners = [];
  11.     },
  12.     
  13.     addListener: function(listener) {
  14.         if (listener) {
  15.             this.listeners.push(listener);
  16.         }
  17.     },
  18.     
  19.     removeListener: function(listener) {
  20.         var found = null;
  21.         for (var i = 0; i < this.listeners.length; ++i) {
  22.             if (this.listeners[i] === listener) {
  23.                 found = i;
  24.             }
  25.         }
  26.         if (found !== null) {
  27.             this.listeners.splice(found,1);
  28.         }
  29.     },
  30.     
  31.     fire: function(callback) {
  32.         var args = Prototype.$A(arguments);
  33.         args.shift();
  34.         if (callback) {
  35.             for (var index = 0; index < this.listeners.length; ++index) {
  36.                 var listener = this.listeners[index];
  37.                 try {
  38.                     if (listener[callback])
  39.                         listener[callback].apply(listener,args);
  40.                 } catch (ex) {
  41.                     if (Components) {
  42.                         Components.utils.reportError(ex);
  43.                     } else {
  44.                         throw ex;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.     }
  50. });
  51.  
  52.